﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.Controllers;

namespace Hisun.View.SCRB
{
    public partial class Scrb_Daily_MainPage : System.Web.UI.Page

    {
        HisunDBDataContext db = new HisunDBDataContext();
         
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var CurrentUserCenterId = (from a in db.TSCRB_JS_Users where a.操作员ID == Session[ServerVars.sysSessionUserName].ToString() select new { a.责任部门ID }).Single();

                DropDownList_CostCenterId.Items.Clear();

                FillData2DropDownList(Convert.ToInt32(CurrentUserCenterId.责任部门ID));

                if (Request["centerid"] != null && Request["PIPid"] != null)
                {
                    string MyCenterId = Request["centerid"].ToString();
                    string MyPIPId = Request["PIPid"].ToString();

                    DropDownList_CostCenterId.SelectedValue = MyCenterId;
                    DropDownList_PIPId.SelectedValue = MyPIPId;

                    GridView1.DataSource = GetDateData();
                    GridView1.DataBind();


                }

            }
        }


        /// <summary>
        ///   查询当前操作员对应的作业中心列表
        /// </summary>
        /// <param name="CenterID"></param>
        private void FillData2DropDownList(int CenterID)
        {
            ChildCenterList myCenter = new ChildCenterList(CenterID);

            var myCenterFilters = myCenter.CostCenterlist.Where(p => new string[] { "BF", "BG", "BZ" }.Contains(p.类型) && p.停用 == false);


            foreach (var MylistItem in myCenterFilters)
            {

                ListItem InsertItem = new ListItem(MylistItem.作业中心名称, MylistItem.作业中心ID.ToString());
                DropDownList_CostCenterId.Items.Add(InsertItem);
            }
            

        }
        /// <summary>
        /// 填充一个datateble 里面有一个日期列
        /// </summary>
        /// <param name="dt_nowday"></param>
        /// <returns></returns>
        private DataTable GetDateData(DateTime dt_nowday )
        {

            int MyMaxDay = 0;
            DataTable dt = new DataTable();
            DataRow dr;
            //查询出允许填写的最大天数值
            var MyMaxDayValue = (from a in db.TSCRB_DM_BasicInfoItems
                            where a.项目编码 == "maxday"
                            select a.项目值).Single();

            if (MyMaxDayValue.ToString() == "")
            {
                return dt;

            }
            else
            {
                MyMaxDay = Convert.ToInt32(MyMaxDayValue);
            
            }

            dt.Columns.Add(new DataColumn("日期", typeof(DateTime)));


            for (int i = (1-MyMaxDay); i <= 0; i++)
            {
                dr = dt.NewRow();
                dr[0] = dt_nowday.AddDays(i);
                dt.Rows.Add(dr);
            }
            return dt;            
        }
        private DataTable GetDateData()
        {
            return GetDateData(DateTime.Today);
        }
 

        protected void Button_Save_Click(object sender, EventArgs e)
        {

        }

        protected void Button_Cancel_Click(object sender, EventArgs e)
        {

        }

        protected void Button_Query_Click(object sender, EventArgs e)
        {
            GridView1.DataSource = GetDateData();
            GridView1.DataBind();
        }

        protected void addbatch(object sender, CommandEventArgs e)
        {
            string str_myday = HttpUtility.UrlEncode(e.CommandArgument.ToString());
          string str_mypip = DropDownList_PIPId.Text;
           string urlarg ="?myday="+str_myday+"&mypip="+str_mypip;
           Response.Redirect("Scrb_daily_newInput.aspx" + urlarg);

        }

        protected void pausebatch(object sender, CommandEventArgs e)
        {
            string str_myday = HttpUtility.UrlEncode(e.CommandArgument.ToString());
            string str_mypip = DropDownList_PIPId.Text;
            string urlarg = "?myday=" + str_myday + "&mypip=" + str_mypip;
            Response.Redirect("Scrb_Daily_PAUSE.aspx" + urlarg);

        }

        protected void Finishbatch(object sender, CommandEventArgs e)
        {

            string str_myday = HttpUtility.UrlEncode(e.CommandArgument.ToString());
            string str_mypip = DropDownList_PIPId.Text;
            string urlarg = "?myday=" + str_myday + "&mypip=" + str_mypip;
            Response.Redirect("Scrb_Daily_Finish.aspx" + urlarg);
        }

        protected void Diliverbatch(object sender, CommandEventArgs e)
        {
            string str_myday = HttpUtility.UrlEncode(e.CommandArgument.ToString());
            string str_mypip = DropDownList_PIPId.Text;
            string urlarg = "?myday=" + str_myday + "&mypip=" + str_mypip;
            Response.Redirect("Scr_Daily_Diliver.aspx" + urlarg);

        }

        protected void Reloadbatch(object sender, CommandEventArgs e)
        {

            string str_myday = HttpUtility.UrlEncode(e.CommandArgument.ToString());
            string str_mypip = DropDownList_PIPId.Text;
            string urlarg = "?myday=" + str_myday + "&mypip=" + str_mypip;
            Response.Redirect("Scrb_Daily_Reload.aspx" + urlarg);
        }

        protected void Collectbatch(object sender, CommandEventArgs e)
        {
            string str_myday = HttpUtility.UrlEncode(e.CommandArgument.ToString());
            string str_mypip = DropDownList_PIPId.Text;
            string urlarg = "?myday=" + str_myday + "&mypip=" + str_mypip;
            Response.Redirect("Scrb_Daily_Collect.aspx" + urlarg);

        }

    }
}
